package com.bachelor.community.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.bachelor.community.mapper.AnswerMapper;
import com.bachelor.community.mapper.QuestionMapper;
import com.bachelor.community.model.entity.Answer;
import com.bachelor.community.model.entity.Question;
import com.bachelor.community.model.vo.AnswerVO;
import com.bachelor.community.service.intf.AnswerService;
import com.bachelor.community.utils.UserSupport;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {
    @Resource
    private AnswerMapper answerMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserSupport userSupport;

    @Override
    public List<AnswerVO> listAnswers(Long questionId, String orderColumn) {
        QueryWrapper<AnswerVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(orderColumn);
        List<AnswerVO> answers = answerMapper.listAnswers(questionId, queryWrapper);
        Question question = questionMapper.selectById(questionId);
        question.setAnswerCount(answers.size());
        questionMapper.updateById(question);
        return answers;
    }

    @Override
    public IPage<AnswerVO> listAnswersByUserId(Integer currentPage, Integer pageSize, Integer userId) {
        IPage<AnswerVO> page = new Page<>(currentPage, pageSize);
        return answerMapper.listAnswersByUserId(page, userId);
    }

    @Override
    public Answer partialUpdateAnswer(Integer answerId, Answer answer) {
        Answer one = getById(answerId);
        one.setUpdateTime(DateUtil.date().toTimestamp());
        one.setContent(answer.getContent());
        one.setIsAccepted(answer.getIsAccepted());
        one.setModifiedCode(answer.getModifiedCode());
        one.setErrorCode(answer.getErrorCode());
        one.setErrorType(answer.getErrorType());
        one.setErrorRow(answer.getErrorRow());
        one.setErrorDesc(answer.getErrorDesc());
        one.setErrorColumn(answer.getErrorColumn());
        one.setCodeBeforeChange(answer.getCodeBeforeChange());
        one.setCodeAfterChange(answer.getCodeAfterChange());
        updateById(one);
        return one;
    }

    @Override
    public IPage<AnswerVO> listAnswersByKeyword(int currentPage, int pageSize, String orderColumn, String keyword) {
        QueryWrapper<AnswerVO> queryWrapper = new QueryWrapper<>(); // 此处无法再用lambdaQueryWrapper, 会报缓存错误
        queryWrapper.orderByDesc("is_top");
        if (orderColumn.equals("hot")) {
            queryWrapper.orderByDesc("view_count");
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        IPage<AnswerVO> page = new Page<>(currentPage, pageSize);
        answerMapper.listAnswersByKeyword(page, queryWrapper, keyword);
        return page;
    }

    @Override
    public AnswerVO insertAnswer(Long questionId, Answer answer) {
        Integer answerer = userSupport.getCurrentUserId();
        answer.setAnswerer(answerer);
        answer.setQuestionId(questionId);
        answer.setCreateTime(DateUtil.date().toTimestamp());
        save(answer);
        AnswerVO answerVO = new AnswerVO();
        BeanUtil.copyProperties(answer, answerVO, true);
        return answerVO;
    }


}
